﻿using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using Heima.OA.UI.Portal.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Heima.OA.UI.Portal.Areas.Check.Controllers
{
    public class CheckFamilyInfoController : BaseController
    {
        public IZS_CheckFamilyInfoService ZS_CheckFamilyInfoService { get; set; }
        public IZS_CheckMemberService ZS_CheckMemberService { get; set; }
        // GET: Declare/FamilyInfo
        public string Index(PaginationQuery paginationQuery)
        {
            int total = 0;
            var familyList = ZS_CheckFamilyInfoService.GetPageEntities(paginationQuery.pagesize, paginationQuery.pageindex, out total, u => u.DelFlag == delflagNormal,
                s => s.CreateTime, true).AsQueryable();

            PaginationData data = new PaginationData() { total = total, rows = JsonHandler.ToJson(familyList) };
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(data));

            return JsonHandler.ToJson(msg);
        }

        #region GetList
        public string GetList()
        {
            var userList = ZS_CheckFamilyInfoService.GetEntities(u => u.DelFlag == 0).AsQueryable();
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(userList));

            return JsonHandler.ToJson(msg);
        }
        /// <summary>
        /// 通过家庭成员名称 模糊检索 获取所有检索到家庭信息
        /// </summary>
        /// <param name="MemberName">模糊检索</param>
        /// <returns></returns>
        public string GetFamily(string MemberName)
        {
            string res = string.Empty;
            JsonMessage msg = new JsonMessage();
            if (!string.IsNullOrEmpty(MemberName))
            {
                IQueryable<ZS_CheckMember> memberList = ZS_CheckMemberService.GetEntities(u => u.DelFlag == delflagNormal && u.MemberName.Contains(MemberName)).AsQueryable<ZS_CheckMember>();
                List<ZS_CheckFamilyInfo> familyList = new List<ZS_CheckFamilyInfo>();
                foreach (var item in memberList)
                {
                    ZS_CheckFamilyInfo family = ZS_CheckFamilyInfoService.GetEntities(u => u.DelFlag == 0 && u.Id == item.CheckFamilyId).FirstOrDefault();
                    if (family != null && !familyList.Exists(f => f.Id == family.Id))
                    {
                        familyList.Add(family);
                    }
                }
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(familyList));
            }
            else
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", "[]");
            }
            return JsonHandler.ToJson(msg);
        }
        /// <summary>
        /// 通过家庭id获取家庭的信息 与家庭成员信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public string GetFamilyMembers(int Id)
        {
            string res = string.Empty;
            ZS_CheckFamilyInfo familyInfo = ZS_CheckFamilyInfoService.GetModel(Id);
            JsonMessage msg = new JsonMessage();
            if (familyInfo != null)
            {
                var memberList = ZS_CheckMemberService.GetEntities(u => u.DelFlag == delflagNormal && u.CheckFamilyId == Id);
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(new { familyInfo = familyInfo, memberList = memberList }));
            }
            else
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "获取id错误");
            }
            res = JsonHandler.ToJson(msg);
            return res;
        }
        /// <summary>
        /// 通过家庭 ID 获取家庭成员信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public string GetMemberList(int Id)
        {
            var list = ZS_CheckMemberService.GetEntities(u => u.DelFlag == delflagNormal && u.CheckFamilyId == Id);
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(list));

            return JsonHandler.ToJson(msg);
        }

        #endregion

        #region 添加家庭信息
        public string Create(ZS_CheckFamilyInfo familyInfo)
        {
            familyInfo.DelFlag = delflagNormal;
            familyInfo.CreateTime = DateTime.Now;
            familyInfo.DeclareFamilyId = 0;

            ZS_CheckFamilyInfoService.Add(familyInfo);
            JsonMessage message = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "添加成功!");
            return JsonHandler.ToJson(message);
        }

        #endregion
        #region 编辑
        public string Edit(ZS_CheckFamilyInfo familyInfo)
        {
            ZS_CheckFamilyInfo editFamilyInfo = ZS_CheckFamilyInfoService.GetModel(familyInfo.Id);
            string res = string.Empty;
            if (editFamilyInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑错误!"));
            }
            else
            {
                editFamilyInfo.FamilyCode = familyInfo.FamilyCode;
                editFamilyInfo.SpouseMemberId = familyInfo.SpouseMemberId;
                editFamilyInfo.SpouseMemberName = familyInfo.SpouseMemberName;
                editFamilyInfo.HuBie = familyInfo.HuBie;
                editFamilyInfo.PowerMemberName = familyInfo.PowerMemberName;
                editFamilyInfo.PowerMemberId = familyInfo.PowerMemberId;
                editFamilyInfo.FamilyAddress = familyInfo.FamilyAddress;
                editFamilyInfo.RegisterNumber = familyInfo.RegisterNumber;

                bool success = ZS_CheckFamilyInfoService.Update(editFamilyInfo);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "编辑成功!"));
                }
            }

            return res;
        }
        #endregion
        #region 删除
        public string Delete(int id)
        {
            ZS_CheckFamilyInfo familyInfo = ZS_CheckFamilyInfoService.GetModel(id);
            string res = string.Empty;
            if (familyInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "删除错误!"));
            }
            else
            {
                familyInfo.DelFlag = (int)DelFlagEnum.Deleted;
                bool success = ZS_CheckFamilyInfoService.Update(familyInfo);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!"));
                }
            }
            return res;
        }
        #endregion

    }
}